package net.yla.smartbus.core.util.vc;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
 * 字节转换器
 * @author ZengXiaoLiang
 * @date 2019/8/1 14:16
*/
public class TransfUtil {

    //类初始化时，不初始化这个对象(延时加载，真正用的时候再创建)
    private static TransfUtil instance;

    //构造器私有化
    private TransfUtil(){}

    //方法同步，调用效率低
    public static synchronized TransfUtil getInstance(){
        if(instance==null){
            instance=new TransfUtil();
        }
        return instance;
    }

    /**
     * 字节转MB
     * */
    public Long byteZhMb(long value){
        return value / 1024 / 1024;
    }

    /**
     * MB转字节
     * */
    public Long mbZhbyte(long value){
        return value * 1024 * 1024;
    }

    /**
     * GB 转字节
     * */
    public Long gbZhbyte(long value){
        return value * 1024 * 1024 * 1024;
    }

    /**
     * 字节转GB
     * */
    public Long byteZhGb(long value){
        return value / 1024 / 1024 / 1024;
    }

    /**
     * 获取当月第一天
     * 响应示例 2019-07-01
     * */
    public Date thatMonthFirstDay() {
        //规定返回日期格式
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();
        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        //设置为第一天
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        //返回本月第一天
        return gcLast.getTime();
    }

    /**
     * 获取今天前一天
     * 响应示例 2019-07-25
     * */
    public Date theDayBeforeToday(){
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }
}
